<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ace Editor with Copy Button</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        #editor {
            height: 200px;
            width: 100%;
        }
        .copy-btn {
            margin-top: 10px;
            cursor: pointer;
            padding: 10px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
        }
        .copy-btn:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>

    <div id="editor">function hello() { console.log("Hello, World!"); }</div>
    <button class="copy-btn" id="copyBtn"><i class="fas fa-copy"></i> 复制代码</button>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/ace.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/theme-monokai.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-javascript.js"></script>
    <script>
        const editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/javascript");

        document.getElementById('copyBtn').addEventListener('click', function() {
            // 获取编辑器中的代码内容
            const code = editor.getValue();

            // 创建一个临时的textarea元素，用于复制代码
            const tempTextarea = document.createElement('textarea');
            tempTextarea.value = code;
            document.body.appendChild(tempTextarea);

            // 选中代码并复制到剪贴板
            tempTextarea.select();
            document.execCommand('copy');

            // 移除临时的textarea元素
            document.body.removeChild(tempTextarea);

            // 提示用户代码已复制
            alert('代码已复制到剪贴板！');
        });
    </script>

</body>
</html>
